import {createMachine} from "xstate";

const ptt_fsm = createMachine({
    "id": "audio (restored)",
    "initial": "idle",
    "states": {
        "idle": {
            "entry": "onIdleEntry",
            "exit": "onIdleExit",
            "on": {
                "FSM_SINGLE_PLAY_EVT": {
                    "target": "single",
                    "actions": "playSingFromIdle"
                },
                "FSM_STREAM_PLAY_EVT": {
                    "target": "stream",
                    "actions": "playStreamFromIdle"
                },
                "FSM_REQUEST_TALK_EVT": {
                    "target": "talk"
                }
            }
        },
        "single": {
            "invoke": {
                "src": "playSingle",
                "id": "single_play",
                "onDone": [
                    {
                        "target": "single_playing",
                        "actions": "onSinglePlayDone"
                    }
                ],
                "onError": [
                    {
                        "target": "idle",
                        "actions": "onSinglePlayError"
                    }
                ]
            }
        },
        "stream": {
            "invoke": {
                "src": "playStream",
                "id": "stream_play",
                "onDone": [
                    {
                        "target": "stream_playing",
                        "actions": "onStreamPlaySucc"
                    }
                ],
                "onError": [
                    {
                        "target": "idle",
                        "actions": "onStreamPlayError"
                    }
                ]
            }
        },
        "talk": {
            "invoke": {
                "src": "startTalk",
                "id": "talk_req",
                "onDone": [
                    {
                        "target": "talking",
                        "actions": "onTalkReqSucc"
                    }
                ],
                "onError": [
                    {
                        "target": "idle",
                        "actions": "onTalkReqError"
                    }
                ]
            }
        },
        "talking": {
            "entry": "onTalkingEntry",
            "exit": "onTalkingExit",
            "on": {
                "FSM_TALK_STOP_EVT": {
                    "target": "talk_stopping"
                },
                "FSM_TALK_ERR_EVT": {
                    "target": "talk_stopping"
                }
            }
        },
        "single_playing": {
            "entry": "onSinglePlayingEntry",
            "exit": "onSinglePlayingExit",
            "on": {
                "FSM_SINGLE_PLAY_EVT": {
                    "target": "single_stopping",
                    "actions": "playSingleFromSingle"
                },
                "FSM_REQUEST_TALK_EVT": {
                    "target": "single_stopping",
                    "actions": "reqTalkFromSingle"
                },
                "FSM_PLAY_STOP_EVT": {
                    "target": "single_stopping"
                },
                "FSM_PLAY_END_EVT": {
                    "target": "single_stopping"
                },
                "FSM_FEED_DATA_EVT": {
                    "actions": "onFeedDataAct"
                }
            }
        },
        "stream_playing": {
            "entry": "onStreamPlayingEntry",
            "exit": "onStreamPlayingExit",
            "on": {
                "FSM_PLAY_NEXT_EVT": {
                    "target": "stream_player_stopping",
                    "actions": "onStreamPlayNextAct"
                },
                "FSM_REQUEST_TALK_EVT": {
                    "target": "stream_player_stopping",
                    "actions": "reqTalkFromStream"
                },
                "FSM_SINGLE_PLAY_EVT": {
                    "target": "stream_player_stopping",
                    "actions": "playSingleFromStream"
                },
                "FSM_FEED_DATA_EVT": {
                    "actions": "onFeedDataAct"
                },
                "FSM_PLAY_END_EVT": {
                    "target": "stream_player_stopping"
                }
            }
        },
        "single_stopping": {
            "invoke": {
                "src": "stopSinglePlayer",
                "id": "single_stop",
                "onDone": [
                    {
                        "target": "single_stop",
                        "actions": "onSingleStopSucc"
                    }
                ],
                "onError": [
                    {
                        "target": "idle",
                        "actions": "onSingleStopError"
                    }
                ]
            }
        },
        "single_stop": {
            "entry": "onSingleStopEntry",
            "exit": "onSingleStopExit",
            "on": {
                "FSM_SINGLE_PLAY_EVT": {
                    "target": "single"
                },
                "FSM_REQUEST_TALK_EVT": {
                    "target": "talk"
                },
                "FSM_IDLE_EVT": {
                    "target": "idle"
                }
            }
        },
        "talk_stopping": {
            "invoke": {
                "src": "stopTalk",
                "id": "stop_talk",
                "onDone": [
                    {
                        "target": "idle",
                        "actions": "onTalkStopSucc"
                    }
                ],
                "onError": [
                    {
                        "target": "idle",
                        "actions": "onTalkStopError"
                    }
                ]
            }
        },
        "stream_player_stopping": {
            "invoke": {
                "src": "streamStop",
                "id": "stream_stop",
                "onDone": [
                    {
                        "target": "stream_player_stop",
                        "actions": "onStreamPlayStopSucc"
                    }
                ],
                "onError": [
                    {
                        "target": "stream_player_stop",
                        "actions": "onStreamPlayStopError"
                    }
                ]
            }
        },
        "stream_player_stop": {
            "entry": "onStreamPlayerStopEntry",
            "on": {
                "FSM_REQUEST_TALK_EVT": {
                    "target": "talk"
                },
                "FSM_SINGLE_PLAY_EVT": {
                    "target": "single"
                },
                "FSM_STREAM_PLAY_EVT": {
                    "target": "stream"
                },
                "FSM_IDLE_EVT": {
                    "target": "idle"
                }
            }
        }
    },
    "schema": {events: {} as {type: 'FSM_SINGLE_PLAY_EVT'} | {type: 'FSM_STREAM_PLAY_EVT'} | {type: 'FSM_REQUEST_TALK_EVT'} | {type: 'FSM_PLAY_STOP_EVT'} | {type: 'FSM_TALK_STOP_EVT'} | {type: 'FSM_PLAY_END_EVT'} | {type: 'FSM_PLAY_NEXT_EVT'} | {type: 'FSM_FEED_DATA_EVT'} | {type: 'FSM_TALK_ERR_EVT'} | {type: 'FSM_IDLE_EVT'}},
    "predictableActionArguments": true,
    "preserveActionOrder": true
})

const FSM_PTT_EVENT_CONSTANT = {
    FSM_SINGLE_PLAY_EVT: "FSM_SINGLE_PLAY_EVT",
    FSM_STREAM_PLAY_EVT: "FSM_STREAM_PLAY_EVT",
    FSM_REQUEST_TALK_EVT: "FSM_REQUEST_TALK_EVT",
    FSM_PLAY_STOP_EVT: "FSM_PLAY_STOP_EVT",
    FSM_TALK_STOP_EVT: "FSM_TALK_STOP_EVT",
    FSM_PLAY_END_EVT: "FSM_PLAY_END_EVT",
    FSM_PLAY_NEXT_EVT: "FSM_PLAY_NEXT_EVT",
    FSM_STREAM_END_EVT: "FSM_STREAM_END_EVT",
    FSM_FEED_DATA_EVT: "FSM_FEED_DATA_EVT",
    FSM_TALK_ERR_EVT: "FSM_TALK_ERR_EVT",
    FSM_IDLE_EVT: "FSM_IDLE_EVT"
};

enum FSM_NODE {
    FSM_IDLE_NODE,
    FSM_SINGLE_PLAY_NODE,
    FSM_STREAM_PLAY_NODE,
    FSM_TALK_NODE
}

export {ptt_fsm, FSM_PTT_EVENT_CONSTANT, FSM_NODE};
